package com.example.starterdemo.config;


import com.example.starterdemo.service.StringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @ConditionalOnClass，当classpath下发现该类的情况下进行自动配置。
 *
 * @ConditionalOnMissingBean，当Spring 容器中不存在该Bean时创建该bean。
 *
 * @ConditionalOnProperty(prefix = "example.service",value = "enabled",havingValue = "true")，
 * 当配置文件中example.service.enabled=true时，创建该bean。
 *
 */
@Configuration
@ConditionalOnClass(StringService.class)
@EnableConfigurationProperties(StringStarterProperties.class)
public class StringStarterAutoConfigure {

    @Autowired
    private StringStarterProperties properties;

    @Bean
    @ConditionalOnMissingBean(StringService.class)
    @ConditionalOnProperty(prefix = "example.service", value = "enabled", havingValue = "true")
    public StringService stringService(){
        return new StringService(properties.getConfig());
    }
}
